package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class SettingActivity extends AppCompatActivity {
    private EditText dollarInput, euroInput, wonInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        // 初始化输入框
        dollarInput = findViewById(R.id.edit_dollar);
        euroInput = findViewById(R.id.edit_euro);
        wonInput = findViewById(R.id.edit_won);

        // 获取初始汇率值
        Intent intent = getIntent();
        dollarInput.setText(String.valueOf(intent.getFloatExtra("dollar_key", 0.14f)));
        euroInput.setText(String.valueOf(intent.getFloatExtra("euro_key", 0.13f)));
        wonInput.setText(String.valueOf(intent.getFloatExtra("won_key", 202.03f)));
    }

    // 保存按钮点击事件
    public void save(View view) {
        try {
            // 获取输入值
            float newDollar = Float.parseFloat(dollarInput.getText().toString());
            float newEuro = Float.parseFloat(euroInput.getText().toString());
            float newWon = Float.parseFloat(wonInput.getText().toString());

            // 返回新汇率
            Intent result = new Intent();
            result.putExtra("dollar_key", newDollar);
            result.putExtra("euro_key", newEuro);
            result.putExtra("won_key", newWon);
            setResult(RESULT_OK, result);
            finish();
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效数字", Toast.LENGTH_SHORT).show();
        }
    }
}